import { BaseInjectHandler } from "../BaseInjectHandler";
import { EMessageC2C } from "../../../common/EMessageC2C";
import { HandleQueryObjList } from "./HandleQueryObjList";
import { TransPack } from "../../../server/framework/common/TransPack";
import { BaseCmpData } from "../common/BaseCmpData";
import { NodeDetailHelper } from "../action/NodeDetailHelper";

/**
 * 处理obj详细信息查询
 */
export class HandleQueryObjDetail extends BaseInjectHandler {

    msgId: number = EMessageC2C.QueryObjDetail;

    private _listHandle: HandleQueryObjList;

    _OnInit() {
        this._listHandle = this.owner.GetHandleById(EMessageC2C.QueryObjList) as HandleQueryObjList;
    }

    _OnHandleMsg(msg: TransPack) {
        let getId = msg.data as number;
        let getNode = this._listHandle.GetNodeById(getId);
        let detailData = new DetailData();
        if (getNode == null || getNode.destroyed) {
            detailData.code = -1;
            this.owner.SendTransOtherData(EMessageC2C.QueryObjDetail_Back, detailData);
            this._listHandle.RemoveNodeById(getId);
            return;
        }
        if (getNode.parent == null && getNode != Laya.stage) {
            detailData.code = -1;
            this.owner.SendTransOtherData(EMessageC2C.QueryObjDetail_Back, detailData);
            return;
        }

        detailData.code = 0;
        detailData.objId = getId;
        detailData.cmps.push(NodeDetailHelper.WrapBaseDetail(getNode));
        if (getNode instanceof Laya.Scene3D) {
            let scene3d = getNode as Laya.Scene3D;
            detailData.cmps.push(NodeDetailHelper.WrapScene3D(scene3d));
        }
        else if (getNode instanceof Laya.Sprite3D) {
            let sp3d = getNode as Laya.Sprite3D;
            detailData.cmps.push(NodeDetailHelper.WrapTransform3D(sp3d.transform));

            if (getNode instanceof Laya.Camera) {
                detailData.cmps.push(NodeDetailHelper.WrapCamera(getNode as Laya.Camera));
            }

        }

        this.owner.SendTransOtherData(EMessageC2C.QueryObjDetail_Back, detailData);

    }

}

export class DetailData {
    /**
     * -1:失败
     */
    code: number = -1;
    /**
     * objId
     */
    public objId: number;
    public cmps: BaseCmpData[] = [];
}